package com.innodealing.process;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

public class GlobalStateAggregator extends RichMapFunction<Tuple2<String, Long>, String> {
    private static final Logger logger = LoggerFactory.getLogger(GlobalStateAggregator.class);

    private final ConcurrentHashMap<String, Long> globalState = new ConcurrentHashMap<>();

    @Override
    public String map(Tuple2<String, Long> value) {
        // 汇总所有分区的状态
        globalState.put(value.f0, value.f1);

        // 打印全局状态
        logger.info("【全局状态】{}", globalState);
        return "Global State: " + globalState;
    }
}
